package com.ke.jun.view.widght

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.ke.jun.view.R

// <attr name="outerColor" format="color"/>
//        <attr name="interColor" format="color"/>
//        <attr name="textsColor" format="color"/>
//        <attr name="textsSize" format="dimension"/>
//        <attr name="ringWidthSize" format="dimension"/>

class RunningView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr){
    private var outerColor: Int
    private var interColor: Int
    private var textsColor: Int
    private var textsSzie: Float
    private var ringWidthSize: Float
    private val interPaint=Paint()//背景的画笔
    private val outterPaint=Paint()//背景的画笔
    private val  textPaint=Paint()//文字的画笔
    var stpet=1f
    set(value) {
        field = value
        invalidate()
    }


    private val totol=3000f;

    init {
        val attrrs= context.obtainStyledAttributes(R.styleable.RunningView)
        outerColor=  attrrs.getColor(R.styleable.RunningView_outerColor,Color.BLUE)
        interColor=  attrrs.getColor(R.styleable.RunningView_interColor,Color.RED)
        textsColor=  attrrs.getColor(R.styleable.RunningView_interColor,Color.RED)
        textsSzie=  attrrs.getDimension(R.styleable.RunningView_textsSize,60f)
        ringWidthSize=  attrrs.getDimension(R.styleable.RunningView_ringWidthSize,20f)

        interPaint.apply {
            isAntiAlias=true
            color=interColor
            style=Paint.Style.STROKE
            strokeWidth=ringWidthSize
            strokeCap=Paint.Cap.ROUND
        }


        outterPaint.apply {
            isAntiAlias=true
            color=outerColor
            style=Paint.Style.STROKE
            strokeWidth=ringWidthSize
            strokeCap=Paint.Cap.ROUND
        }

        textPaint.apply {
            isAntiAlias=true
            color=textsColor
            style=Paint.Style.FILL
            textSize=textsSzie
        }
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //画里面的圆弧
        val rect= RectF(ringWidthSize/2, ringWidthSize/2, width-ringWidthSize/2, height-ringWidthSize/2)
        canvas.drawArc(rect,135f,270f,false,interPaint)
        canvas.drawArc(rect,135f,stpet/totol *270f,false,outterPaint)
        val bounds = Rect()
        var fontMetrics = textPaint.fontMetrics
        textPaint.getTextBounds(stpet.toString(),0,stpet.toString().length,bounds)
        var baseline =  ((bounds.bottom - bounds.top) / 2+ (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom)
        canvas.drawText(stpet.toInt().toString(), (width-bounds.width())/2f, height/2-baseline, textPaint);



    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(widthSize.coerceAtMost(heightSize), widthSize.coerceAtMost(heightSize))
    }



}